<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="title">服装定制管理系统</h1>
      
      <el-tabs v-model="activeTab" class="login-tabs">
        <el-tab-pane label="登录" name="login">
          <el-form 
            ref="loginFormRef" 
            :model="loginForm" 
            :rules="loginRules" 
            class="login-form"
          >
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="用户名"
              >
                <template #prefix>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                type="password" 
                placeholder="密码"
                show-password
              >
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="role">
              <el-select
                v-model="loginForm.role"
                placeholder="选择角色"
                style="width: 100%"
              >
                <el-option label="普通用户" value="user" />
                <el-option label="设计师" value="designer" />
                <el-option label="管理员" value="admin" />
              </el-select>
            </el-form-item>
            
            <el-form-item>
              <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
              <a href="javascript:;" class="forgot-password">忘记密码？</a>
            </el-form-item>
            
            <el-button 
              type="primary" 
              :loading="loading" 
              class="login-button" 
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="注册" name="register">
          <el-form 
            ref="registerFormRef" 
            :model="registerForm" 
            :rules="registerRules" 
            class="register-form"
          >
            <el-form-item prop="username">
              <el-input 
                v-model="registerForm.username" 
                placeholder="用户名"
              >
                <template #prefix>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input 
                v-model="registerForm.password" 
                type="password" 
                placeholder="密码"
                show-password
              >
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input 
                v-model="registerForm.confirmPassword" 
                type="password" 
                placeholder="确认密码"
                show-password
              >
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input 
                v-model="registerForm.email" 
                placeholder="邮箱"
              >
                <template #prefix>
                  <el-icon><Message /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item prop="phone">
              <el-input 
                v-model="registerForm.phone" 
                placeholder="手机号"
              >
                <template #prefix>
                  <el-icon><Phone /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-button 
              type="primary" 
              :loading="loading" 
              class="register-button" 
              @click="handleRegister"
            >
              注册
            </el-button>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'

export default {
  name: 'Login',
  components: {
    User,
    Lock,
    Message,
    Phone
  },
  setup() {
    const router = useRouter()
    const activeTab = ref('login')
    const loading = ref(false)
    
    // 登录表单
    const loginFormRef = ref(null)
    const loginForm = reactive({
      username: '',
      password: '',
      role: '',
      remember: false
    })
    
    // 登录表单验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在3到20个字符之间', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不少于6个字符', trigger: 'blur' }
      ],
      role: [
        { required: true, message: '请选择角色', trigger: 'change' }
      ]
    }
    
    // 处理登录
    const handleLogin = () => {
      loginFormRef.value.validate((valid) => {
        if (valid) {
          loading.value = true
          
          // 模拟登录请求
          setTimeout(() => {
            // 保存用户信息和token到本地存储
            localStorage.setItem('token', 'mock_token_' + Date.now())
            localStorage.setItem('userRole', loginForm.role)
            localStorage.setItem('username', loginForm.username)
            
            // 根据角色重定向到不同页面
            if (loginForm.role === 'user') {
              router.push('/home')
            } else if (loginForm.role === 'designer') {
              router.push('/designer/dashboard')
            } else if (loginForm.role === 'admin') {
              router.push('/admin/dashboard')
            }
            
            ElMessage.success('登录成功')
            loading.value = false
          }, 1000)
        }
      })
    }
    
    // 注册表单
    const registerFormRef = ref(null)
    const registerForm = reactive({
      username: '',
      password: '',
      confirmPassword: '',
      email: '',
      phone: ''
    })
    
    // 注册表单验证规则
    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在3到20个字符之间', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不少于6个字符', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        { 
          validator: (rule, value, callback) => {
            if (value !== registerForm.password) {
              callback(new Error('两次输入的密码不一致'))
            } else {
              callback()
            }
          }, 
          trigger: 'blur' 
        }
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
      ]
    }
    
    // 处理注册
    const handleRegister = () => {
      registerFormRef.value.validate((valid) => {
        if (valid) {
          loading.value = true
          
          // 模拟注册请求
          setTimeout(() => {
            ElMessage.success('注册成功，请登录')
            activeTab.value = 'login'
            loading.value = false
          }, 1000)
        }
      })
    }
    
    return {
      activeTab,
      loading,
      loginFormRef,
      loginForm,
      loginRules,
      handleLogin,
      registerFormRef,
      registerForm,
      registerRules,
      handleRegister
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
  background-size: cover;
  background-position: center;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  font-size: 24px;
  color: #333;
  margin-bottom: 30px;
}

.login-tabs {
  margin-bottom: 20px;
}

.login-form,
.register-form {
  margin-top: 20px;
}

.login-button,
.register-button {
  width: 100%;
  margin-top: 10px;
}

.forgot-password {
  float: right;
  color: #409EFF;
  font-size: 14px;
  text-decoration: none;
}

.forgot-password:hover {
  text-decoration: underline;
}
</style> 